package me.firstapp.spi.shop.impl;

import java.util.HashMap;
import java.util.Map;

import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;

import me.firstapp.common.exception.ServiceException;
import me.firstapp.common.exception.StatusHouse;
import me.firstapp.common.json.SingleObject;
import me.firstapp.common.module.shop.JsonShoppingCart;
import me.firstapp.spi.base.impl.BaseServiceImpl;
import me.firstapp.spi.shop.ShoppingCartService;

@Service
public class ShoppingCartServiceImpl extends BaseServiceImpl implements ShoppingCartService {

	@Override
	public SingleObject<Object> updateShoppingCart(Long shoppingCartId, String params) {
		SingleObject<Object> result = new SingleObject<Object>();
		try {
			Map<String, Object> _params = new HashMap<String, Object>();
			_params.put("params", params);

			Map<String, Object> parameters = new HashMap<>();
			parameters.put("shoppingCartId", shoppingCartId);

			JSONObject jsonObject = apiBody(parameters, "/shop/updateShoppingCart.do", "更新购物车", _params);
			if (StatusHouse.COMMON_STATUS_OK.getCode().equals(jsonObject.getString("code"))) {
				result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
			} else {
				throw new ServiceException(jsonObject.getString("code"), jsonObject.getString("msg"));
			}

		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		return result;
	}

	@Override
	public SingleObject<JsonShoppingCart> findMemberShoppingCart(Long memberId) {
		SingleObject<JsonShoppingCart> result = new SingleObject<JsonShoppingCart>();
		try {
			Map<String, Object> parameters = new HashMap<>();
			parameters.put("memberId", memberId);
			JSONObject resultObject = api(parameters, "/shop/findMemberShoppingCart.do", HttpMethod.GET, "获取用户购物车");

			if (StatusHouse.COMMON_STATUS_OK.getCode().equals(resultObject.getString("code"))) {
				JSONObject jsonObject = resultObject.getJSONObject("object");
				JsonShoppingCart cart = JSONObject.toJavaObject(jsonObject, JsonShoppingCart.class);
				result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
				result.setObject(cart);
			} else {
				throw new ServiceException(resultObject.getString("code"), resultObject.getString("msg"));
			}
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		return result;
	}

	@Override
	public SingleObject<Object> deleteShoppingCartProduct(Long shoppingCartId, String productIds, String productSkuRelIds) {
		SingleObject<Object> result = new SingleObject<Object>();
		try {
			Map<String, Object> parameters = new HashMap<>();
			parameters.put("shoppingCartId", shoppingCartId);
			parameters.put("productIds", productIds);
			parameters.put("productSkuRelIds", productSkuRelIds);
			JSONObject resultObject = api(parameters, "/shop/deleteShoppingCartProduct.do", HttpMethod.POST,
					"删除购物车中的商品");

			if (StatusHouse.COMMON_STATUS_OK.getCode().equals(resultObject.getString("code"))) {
				result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
			} else {
				throw new ServiceException(resultObject.getString("code"), resultObject.getString("msg"));
			}
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		return result;
	}

}
